{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def partitions(number, k):\n",
    "    '''\n",
    "    Distribution of the folds\n",
    "    Args:\n",
    "        number: number of patients\n",
    "        k: folds number\n",
    "    '''\n",
    "    n_partitions = np.ones(k) * int(number/k)\n",
    "    n_partitions[0:(number % k)] += 1\n",
    "    return n_partitions\n",
    "\n",
    "def get_indices(n_splits = 3, subjects = 145, frames = 20):\n",
    "    '''\n",
    "    Indices of the set test\n",
    "    Args:\n",
    "        n_splits: folds number\n",
    "        subjects: number of patients\n",
    "        frames: length of the sequence of each patient\n",
    "    '''\n",
    "    l = partitions(subjects, n_splits)\n",
    "    fold_sizes = l * frames\n",
    "    indices = np.arange(subjects * frames).astype(int)\n",
    "    current = 0\n",
    "    for fold_size in fold_sizes:\n",
    "        start = current\n",
    "        stop =  current + fold_size\n",
    "        current = stop\n",
    "        yield(indices[int(start):int(stop)])\n",
    "\n",
    "def k_folds(n_splits = 3, subjects = 145, frames = 20):\n",
    "    '''\n",
    "    Generates folds for cross validation\n",
    "    Args:\n",
    "        n_splits: folds number\n",
    "        subjects: number of patients\n",
    "        frames: length of the sequence of each patient\n",
    "    '''\n",
    "    indices = np.arange(subjects * frames).astype(int)\n",
    "    for test_idx in get_indices(n_splits, subjects, frames):\n",
    "        train_idx = np.setdiff1d(indices, test_idx)\n",
    "    yield train_idx, test_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17\n",
      "  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35\n",
      "  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53\n",
      "  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71\n",
      "  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89\n",
      "  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107\n",
      " 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125\n",
      " 126 127 128 129 130 131 132 133 134 135 136 137 138 139]\n"
     ]
    }
   ],
   "source": [
    "for i,j in k_folds(3, 20, 10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 3 8]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 4, 6, 7, 9]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "indices = np.asarray([i for i in range(10)])\n",
    "split = 3\n",
    "validation_idx = np.random.choice(indices, size=split, replace=False)\n",
    "validation_idx\n",
    "print(validation_idx)\n",
    "train_idx = list(set(indices) - set(validation_idx))\n",
    "train_idx"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:miniconda3]",
   "language": "python",
   "name": "conda-env-miniconda3-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
